define([
        'underscore',
        'app/service/GeneralServices',
        'app/utils/WebUtils',
        'app/utils/CommonUtils'
        ], function (
		_,
		GeneralServices,
		WebUtils
    ) {
	
    var s = {};
	
    
    function fixImgHeight(ratio){
    	var w = $(".am-gallery-item-pic").width();
    	$(".am-gallery-item-pic").height(parseInt(w*ratio));
    	
    	var w2 = $(".am-modal-item-pic").width();
    	$(".am-modal-item-pic").height(parseInt(w2*ratio));
    }
    function updateLectureRatio(ratio, useruuid, roomuuid){
    	var p = {
    		useruuid:useruuid,
    		roomuuid:roomuuid,
    		ratio:ratio
    	}
    	GeneralServices.wjt_lecture_ratio_update(p, function(resp){
    		
    	});
    }
    
    function renderNumSelect(num ,id){
    	var disabled = "disabled";
    	if(num > 2){
    		disabled = "";
    		var i = 3;
    	}else{
    		var i = 1;
    	}
    	var el = '<select class="wjt-catalogs-order" '+disabled+' data-id="'+id+'">';
    	
    	for(; i<= 50; i++){
    		if(num == i){
    			el = el + '<option selected="selected" value="'+i+'">'+i+'</option>';
    		}else{
    			el = el + '<option value="'+i+'">'+i+'</option>';
    		}
    	}
    	el = el + '</select>';
    	return el;
    }
    
    function renderOperate(item){
    	
    	var el1 = 
    		'<li><a data-id="'+item.uuid+'" data-name="'+item.name+'"  class="wjt-catalogs-change" href="javascript:void(0);">编辑</a></li>'+
    		'<li><a data-id="'+item.uuid+'" data-name="'+item.name+'"   class="wjt-catalogs-del" href="javascript:void(0);">删除</a></li>';
    	
    	var el2 = 
    		'<li><a>系统设置不能修改</a></li>';
    	
    	
    	if(parseInt(item.ordernum) > 2){
    		return el1;
    	}else{
    		return el2;
    	}
    }
    
    function del_catalog(){
    	var uuid = $(this).data("id");
    	
    	var p ={
    		uuid:uuid
    	};
    	
    	WebUtils.confirm("是否要删除此分类",function(){
    		//OK
        	GeneralServices.catalog_del(p,function(resp){
        		$.refreshPage();
        	});    		
    	},function(){
    		//cancel
    	});
    	
    }
    function order_catalog(){
    	var id = $(this).data("id");
    	var neworder = $(this).val();
    	var p={
    		uuid:id,
    		ordernum:neworder
    	};
    	GeneralServices.catalog_order_update(p,function(resp){
    		if(resp.status == "200"){
    			$.refreshPage();
    		}
    	}); 
    }
    function edit_catalog(){
    	
		$("#wjt_catalog_dialog").modal();
		$("#wjt_add_catalog_input").val($(this).data("name"));
		$("#wjt_catalog_uuid").val($(this).data("id"));
		
    }
    
    function renderPic(lp){
    	if(lp.type == "img"){
    		return lp.pageurl;
    	}else if(lp.type == "video" || lp.type == "live"){
    		return lp.thumburl;
    	}else{
    		return "";
    	}
    }
    
    function renderCatalogs(catalogs){
    	$(".wjt-lecture-gallery").html("");
    	for(var i = 0; i< catalogs.length ; i++){
    		var item = catalogs[i];
    		
    		
//
//    		var el_pic = renderPic(item);
    		var el_num = renderNumSelect(item.ordernum, item.uuid);
//    		var el_type = renderType(item.type);
    		var el_operate = renderOperate(item);
//    		
//    		
    		var el = $(
    			'<li id="lecture_page_'+item.uuid+'">'+
			        '<div class="am-gallery-item">'+
			        	'<h3>'+item.name+'</h3>'+
		                '<h3 class="am-gallery-title">'+
		                	'<div class="am-g">'+
		                		'<div class="am-u-sm-6">排序：'+el_num+'</div>'+
								'<div class="am-u-sm-6" style="text-align:right;">'+
								'</div>'+
		                	'</div>'+
		                '</h3>'+
		                '<div class="am-gallery-desc">'+
							'<div class="am-g">'+
								'<div class="am-u-sm-6" style="text-align: right;margin-top: -20px;"></div>'+
								'<div class="am-u-sm-6" style="text-align: right;margin-top: -20px;">'+
									'<div class="am-dropdown  am-dropdown-up" data-am-dropdown="">'+
					                  '<button class="am-btn am-btn-default am-btn-xs am-dropdown-toggle" data-am-dropdown-toggle=""><span class="am-icon-cog"></span> <span class="am-icon-caret-up"></span></button>'+
					                  '<ul class="am-dropdown-content">'+
					                    el_operate+
					                  '</ul>'+
					                '</div>'+
			                	'</div>'+
		                	'</div>'+
		                '</div>'+
			        '</div>'+
			      '</li>');
    		$(".wjt-lecture-gallery").append(el);
    	}
    	
//    	
    	$(".wjt-catalogs-del").click(del_catalog);
    	$(".wjt-catalogs-change").click(edit_catalog);
    	$(".wjt-catalogs-order").change(order_catalog);
    	
		//下拉栏
		$('.am-dropdown').dropdown();
    }
    
    
    
	s.init=function(data){
		
		$("#wjt_add_catalog_btn").click(function(){
			$("#wjt_catalog_dialog").modal();
			$("#wjt_add_catalog_input").val("");
			$("#wjt_catalog_uuid").val("");
		});
		
		$("#wjt_add_catalog_save").click(function(){
			
			var v = $("#wjt_add_catalog_input").val();
			var uuid = $("#wjt_catalog_uuid").val();
			if($.isNullStr(v)){
				WebUtils.alert("分类名称不能为空");
				return;
			}
			var p={
	    		name:v,
	    		uuid:uuid
	    	};
			if($.isNullStr(uuid)){
				
				GeneralServices.catalog_order_save(p,function(resp){
		    		if(resp.status == "200"){
		    			$.refreshPage();
		    		}else{
		    			if(c.isNullStr(resp.result)){
		    				WebUtils.alert("操作失败，请重试");
		    			}else{
		    				WebUtils.alert(resp.result);
		    			}
		    			
		    		}
		    	}); 
				
			}else{

		    	GeneralServices.catalog_change(p,function(resp){
		    		if(resp.status == "200"){
		    			$.refreshPage();
		    		}else{
		    			if(c.isNullStr(resp.result)){
		    				WebUtils.alert("操作失败，请重试");
		    			}else{
		    				WebUtils.alert(resp.result);
		    			}
		    			
		    		}
		    	}); 
				
			}
	    	
		});
		
		var catalogs = data.catalogs;
		
		renderCatalogs(catalogs);
		
	};
	
	
	return s;

});
